<?php

/*
 * Following code will update a user information
 * A User is identified by user id (uid)
 */

// array for JSON response
$response = array();

// check for required fields
if (isset($_POST['UserID']) && isset($_POST['Username']) && isset($_POST['Password']) && isset($_POST['Firstname']) && isset($_POST['Lastname']) && isset($_POST['Email'])) {
    
    $uid = $_POST['UserID'];
    $username = $_POST['Username'];
    $password = $_POST['Password'];
    $firstname = $_POST['Firstname'];
    $lastname = $_POST['Lastname'];
    $email = $_POST['Email'];

    // include db connect class
    require_once __DIR__ . '/db_connect.php';

    // connecting to db
    $db = new DB_CONNECT();

    // mysql update row with matched uid
    $result = mysql_query("UPDATE User SET Username = '$username', Password = '$password', Firstname = '$firstname', Lastname = '$lastname', Email = '$email' WHERE UserID = $uid");

    // check if row inserted or not
    if ($result) {
        // successfully updated
        $response["success"] = 1;
        $response["message"] = "User successfully updated.";
        
        // echoing JSON response
        echo json_encode($response);
    } else {
        
    }
} else {
    // required field is missing
    $response["success"] = 0;
    $response["message"] = "Required field(s) is missing";
	
    // echoing JSON response
    echo json_encode($response);
}
?>
